function grafterInitialize() {

    var treeSeparation = 50;

    globals.r = Raphael( "raphaelContainer", 300, 100 );

    var maxGenerations = 0; var longestChildLabel = 0; var longestRootLabel = 0; var totalTips = 0; var treeCount = 0;

    for each( var tree in globals.grafterTrees ) {
        tree.treeInfo = getTreeInfo( { tree: tree.tree } );
        if( tree.treeInfo.generations > maxGenerations ) { maxGenerations = tree.treeInfo.generations; }
        if( tree.treeInfo.longestChildLabel > longestChildLabel ) { longestChildLabel = tree.treeInfo.longestChildLabel; }
        if( tree.treeInfo.rootLabelLength > longestRootLabel ) { longestRootLabel = tree.treeInfo.rootLabelLength; }
        totalTips = totalTips + tree.treeInfo.childless;
        treeCount++;
    }

    globals.canvas.width = config.generationSeparation * (maxGenerations-1) + (config.canvasBuffer*2) + longestChildLabel + longestRootLabel;

    globals.canvas.height = ( ( totalTips - 1 ) * config.nodeSeparation ) + ( config.canvasBuffer * 2 ) + ( ( treeCount - 1 ) * treeSeparation );

    if( globals.canvas.width < $('#mainPanel').width() ) { globals.canvas.width = $('#mainPanel').width(); }
    if( globals.canvas.height < $('#mainPanel').height() ) { globals.canvas.height = $('#mainPanel').height(); }

    resetCurrentTip( { scaleValue: { x: 1, y: 1 }, longestChildLabel: longestChildLabel } );

    $('#raphaelContainer').width( globals.canvas.width );
    $('#raphaelContainer').height( globals.canvas.height );
    
    globals.r.setSize( globals.canvas.width, globals.canvas.height );
}

function handlePathDrag( p ) {

    var translation = { x: p.e.pageX - globals.currentEvents.mouseCoords.x, y: p.e.pageY - globals.currentEvents.mouseCoords.y };
    p.tree.path.translate( translation.x, translation.y );
    for each( var label in p.tree.labels ) { label.translate( translation.x, translation.y ); }
    globals.currentEvents.mouseCoords = { x: p.e.pageX, y: p.e.pageY };
}

function requestGrafter() {

    var request = new StringBuffer();
    var divs = $('#savedClades > div.selectedSavedClade');

    $.each( divs, function( index, item ) { request.append( $(item).attr('treeId') ); request.append( (index == divs.length-1) ? '' : '/' ); } );

    window.location = '/' + globals.applicationName + '/default/grafter/' + request.toString();
}
